Skip to content

[FIX] hr_recruitment: fix lift_constraints() constraint drop order for PostgreSQL 17+ - #5884

Closed
quoc-pn wants to merge 1 commit into
OCA:19.0from
komit-consulting:19.0-fix-hr_recruitment-lift_constraints-pg17
Closed

[FIX] hr_recruitment: fix lift_constraints() constraint drop order for PostgreSQL 17+#5884
quoc-pn wants to merge 1 commit into
OCA:19.0from
komit-consulting:19.0-fix-hr_recruitment-lift_constraints-pg17

Conversation

@quoc-pn

@quoc-pn quoc-pn commented Aug 5, 2026

Copy link
Copy Markdown
Member

Bug

On PostgreSQL 17+, NOT NULL is catalogued as a named constraint (<table>_<column>_not_null) instead of an implicit attribute flag on pg_attribute.

openupgrade.lift_constraints() drops all constraints on a column in a single ALTER TABLE ... DROP CONSTRAINT a, DROP CONSTRAINT b statement. When the column has both a not-null constraint and a primary key, and the not-null constraint's drop clause is listed before the primary key's, PostgreSQL rejects the statement:

ERROR: column "id" is in a primary key

PostgreSQL validates each ALTER TABLE subcommand against the catalog state before the statement runs, not against the pending effects of earlier subcommands in the same statement — so even though the very next clause would drop the primary key, the not-null drop is rejected first.

This surfaced in hr_recruitment/19.0.1.1/pre-migration.py when merging hr.candidate into hr.applicant: after renaming hr_candidate to its legacy table, lift_constraints(cr, legacy_table, "id", cascade=True) fails on PostgreSQL 17/18 with the error above.

Fix

Drop the primary key constraint on the legacy table in its own, separate ALTER TABLE ... DROP CONSTRAINT ... CASCADE statement before calling lift_constraints(). By the time lift_constraints() runs, only the not-null constraint remains on the column, so there's no ordering conflict.

Test

Reproduced the failure on a database running PostgreSQL 18.4 during an Odoo 19 upgrade, confirmed the fix resolves it.

@OCA-git-bot OCA-git-bot added mod:openupgrade_scripts Module openupgrade_scripts series:19.0 labels Aug 5, 2026
…r PostgreSQL 17+

On PostgreSQL 17+, NOT NULL is catalogued as a named constraint
(<table>_<column>_not_null) instead of an implicit attribute flag.
openupgrade.lift_constraints() drops all constraints on a column in a
single ALTER TABLE statement, listing the not-null constraint before
the primary key constraint. PostgreSQL validates each subcommand
against the pre-statement catalog state, so it refuses to drop the
not-null constraint while the column is still part of the primary
key, raising:

    ERROR: column "id" is in a primary key

This happened when merging hr.candidate into hr.applicant and lifting
constraints on the renamed legacy table's id column.

Fix by dropping the primary key constraint on its own, in a separate
ALTER TABLE statement, before calling lift_constraints() for the
remaining not-null constraint.
@quoc-pn
quoc-pn force-pushed the 19.0-fix-hr_recruitment-lift_constraints-pg17 branch from b21bfcf to f79477b Compare August 5, 2026 08:00
@pedrobaeza

Copy link
Copy Markdown
Member

Shouldn't this be fixed in openupgradelib instead?

@quoc-pn

quoc-pn commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Shouldn't this be fixed in openupgradelib instead?

@pedrobaeza Thanks for the suggestion. I'll create a PR in openupgradelib.

This PR will be closed once the corresponding PR in openupgradelib has been created.

@quoc-pn

quoc-pn commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Closing this in favor of fixing the root cause upstream in openupgradelib itself: lift_constraints() combined all constraint drops into a single ALTER TABLE statement, which PostgreSQL 17+ rejects when a not-null constraint is listed before the primary key it depends on. See the fix at OCA/openupgradelib#462, which drops each constraint individually.

@quoc-pn quoc-pn closed this Aug 5, 2026
@quoc-pn
quoc-pn deleted the 19.0-fix-hr_recruitment-lift_constraints-pg17 branch August 5, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:openupgrade_scripts Module openupgrade_scripts series:19.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants